package com.csx;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author 陈胤训
 * <p>
 * create: 2019-04-07 15:53
 **/
public class SynchronizedDemo implements Runnable {
    private static int count = 0;

    public static void testA() {
        long start = System.nanoTime();
        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 1000; j++)
                for (int k = 0; k < 10000; k++)
                    ;
        System.out.println("testA time>>" + (System.nanoTime() - start) + "ns");
    }

    public static void testB() {
        long start = System.nanoTime();
        for (int i = 0; i < 10000; i++)
            for (int j = 0; j < 1000; j++)
                for (int k = 0; k < 10; k++)
                    ;
        System.out.println("testB time>>" + (System.nanoTime() - start) + "ns");
    }

    public static void testC() {
        long start = System.nanoTime();
        int i, j, k;
        for (i = 0; i < 10; i++)
            for (j = 0; j < 1000; j++)
                for (k = 0; k < 10000; k++)
                    ;
        System.out.println("testC time>>" + (System.nanoTime() - start) + "ns");

    }

    public static void testD() {
        long start = System.nanoTime();

        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 1000; j++)
                for (int k = 0; k < 10000; k++)
                    ;
        System.out.println("testA time>>" + (System.nanoTime() - start) + "ns");
    }


    public static void main(String[] args) {

        System.out.println(5+(2-5)/2);
        System.out.println((5+2)/2);

//        testA();
//        testB();
//        testC();
        testD();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new SynchronizedDemo());
            thread.start();
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("result: " + count);
    }

    @Override
    public void run() {
        synchronized (SynchronizedDemo.class) {
            for (int i = 0; i < 100000; i++)
                count++;
        }
    }

}